package com.flash.javamate.utils;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @author sunyiming
 * @date 2018/8/13
 */
public class JavaPathHandler {

    public static String getRealPath(String userDir, String classFullName) {
        StringBuilder sb = new StringBuilder();
        String separator = userDir.contains("/") ? "/" : "\\";
        if (userDir.endsWith(separator)) {
            sb.append(userDir);
        } else {
            sb.append(userDir).append(separator);
        }
        String classReferencePath = classFullName.replace(".", separator);
        sb.append(classReferencePath).append(".java");
        return sb.toString();
    }

    public static File getFile(String userDir, String classFullName, String[] userDirs) {
        String realPath = getRealPath(userDir, classFullName);
        File javaFile = null;
        javaFile = new File(realPath);
        if (!javaFile.exists()) {
            for (String dir : userDirs) {
                realPath = JavaPathHandler.getRealPath(dir, classFullName);
                javaFile = new File(realPath);
                if (!javaFile.exists()) {
                    continue;
                }
                break;
            }
        }

        return javaFile;
    }

}
